package Servlet.User;

import Service.FindByPageService;
import Service.SearchByPageService;
import Table.Book;
import Table.PageBean;
import com.alibaba.fastjson.JSON;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/userSearchByPageServlet")
public class UserSearchByPageServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String bookname = req.getParameter("bookname");
        String auther = req.getParameter("auther");
        String currentPage = req.getParameter("currentPage");
        String rows = req.getParameter("rows");
        String totalPage = req.getParameter("totalPage");
        PrintWriter out = resp.getWriter();
        if (currentPage == null || Integer.parseInt(currentPage) <= 0) {
            currentPage = "1";
        }
        if (rows == null) {
            rows = "4";
        }
        if (bookname == null) {
            bookname = "";
        }
        if (auther == null) {
            auther = "";
        }
        if (totalPage != null && Integer.parseInt(currentPage) >= Integer.parseInt(totalPage)) {
            currentPage = totalPage;
        }

        PageBean<Book> pageBean = SearchByPageService.SearchByPage(currentPage, rows, bookname, auther);
        String s = JSON.toJSONString(pageBean);
        //设置编码
        resp.setContentType("application/json;charset=UTF-8");
        out.print(s);

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
